package qjc.j_回溯;

import java.util.ArrayList;
import java.util.List;

public class j3_电话号码的字母组合 {
    private static final String[] DIGIT_TO_LETTERS = {
            "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
    };
    public List<String> letterCombinations(String digits) {
        List<String> list = new ArrayList<String>();
        if (digits == null || digits.length() == 0) {
            return list;
        }
        backtrack(list,new StringBuilder(),digits,0);
        return list;
    }

    private void backtrack(List<String> list, StringBuilder stringBuilder, String digits, int i) {
        if (i == digits.length()) {
            list.add(stringBuilder.toString());
            return;
        }
        char c = digits.charAt(i);
        String letter = DIGIT_TO_LETTERS[c - '0'];
        for(char j:letter.toCharArray()){
            stringBuilder.append(j);
            backtrack(list,stringBuilder,digits,i+1);
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
        }
    }
}
